#!/usr/bin/python3
# -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t -*-

import mrget
import subprocess
import mirrors.plugin


def main():
    cfg = mirrors.plugin.params["config"]
    dataDir = mirrors.plugin.params["storage-file"]["data-directory"]

    def filterFunc(x):
        return x["protocol"] == "rsync" and x["role"] != "local-mirror"
    rsyncSource = mrget.target_urls("mirror://fdroid", filter_key=filterFunc)[0]
    print("sync from %s" % (rsyncSource))

    bArchive = cfg.get("archive", True)
    if not bArchive:
        patternList = [
            "+ /",
            "+ /repo/***",
            "- /**",
        ]
    else:
        patternList = []

    cmd = None
    if True:
        cmd = "/usr/bin/rsync -v -a -z --delete --delete-excluded --partial "
        for p in patternList:
            cmd += "-f '%s' " % (p)
        cmd += "%s %s" % (rsyncSource, dataDir)

    subprocess.run(cmd, shell=True, check=True)


if __name__ == "__main__":
    main()
